package ywpt.web.action.config;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.ecside.table.limit.Limit;
import org.ecside.util.RequestUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import ywpt.core.exception.UniqueConstraintException;
import ywpt.core.web.BaseAction;
import ywpt.model.ContractInfo;

import ywpt.model.UnitInfo;

import ywpt.service.config.UnitInfoService;

import ywpt.service.people.PeopleService;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.sun.jmx.snmp.Timestamp;

public class UnitInfoAction extends BaseAction implements
		ModelDriven<UnitInfo>, Preparable {

	/**
	 * 单位Action
	 */
	private static final long serialVersionUID = -9031703786303989104L;
	private static int DEFAULT_PAGE_SIZE = 20;
	private Long id;
	private String unitName;
	
	private UnitInfo unitInfo;	
	private PeopleService peopleService;
	private UnitInfoService unitInfoService;
	private List<UnitInfo> unitInfoList = new ArrayList<UnitInfo>();


	public PeopleService getPeopleService() {
		return peopleService;
	}

	public void setPeopleService(PeopleService peopleService) {
		this.peopleService = peopleService;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	// ****************************默认方法*******************************

	@Override
	@SuppressWarnings("unchecked")
	public String execute() {
		return null;
	}

	@Override
	public UnitInfo getModel() {
		return this.unitInfo;
	}

	@Override
	public void prepare() throws Exception {
		if (id != null) {
			this.unitInfo = unitInfoService.getUnitInfoById(id);
		} else {
			this.unitInfo = new UnitInfo();
		}
	}

	// **************************数据交互方法*******************************
	public String list() {
		DetachedCriteria query = DetachedCriteria.forClass(UnitInfo.class);
		getDataList(query);
		return SUCCESS;
	}

	public String input() {
		return INPUT;
	}

	public String edit() {
		this.unitInfo = unitInfoService.getUnitInfoById(id);
		return INPUT;
	}
	
	public String save() {
		try {
			if (id == null) {
				unitInfo.setInsert_oper(this.getCurrentUser());
				unitInfo.setInsert_time(new Date() );
				unitInfoService.saveUnitInfo(unitInfo);
				this.addActionMessage("单位信息新增成功!");				
			} else {
					unitInfo.setUpdate_oper( this.getCurrentUser());
					unitInfo.setUpdate_time( new Date() );
					unitInfoService.updateUnitInfo(unitInfo);		
					this.addActionMessage("单位信息更新成功!");		
			}
			return SUCCESS;
		} catch (UniqueConstraintException e) {
				this.addActionError("保存错误:" + e.getMessage());
			return INPUT;
		}
	}

	public String delete() throws Exception {
		if (id != null) {
			DetachedCriteria peopleQuery = DetachedCriteria.forClass(UnitInfo.class);
/*			peopleQuery.add(Restrictions.eq("depart", unitInfoService.getDepartmentById(id)));
			int totalRows2 = peopleService.getPeopleInfoCountByCriteria(peopleQuery);*/

			int totalRows2 =0;
			if (totalRows2>0  ) {
				this.addActionMessage("该单位已经发生过业务逻辑，不能删除！");
				return INPUT;
			}else{
				unitInfo.setUpdate_oper( this.getCurrentUser());
				unitInfo.setUpdate_time( new Date() );
				unitInfo.setDelete_flag(true);
				unitInfoService.saveUnitInfo(unitInfo);
				return SUCCESS;				
			}

		}
		this.addActionMessage("单位编号为空，请选择具体的记录！");
		return INPUT;
	}

	public String select() 
	{
		unitInfoList.addAll(unitInfoService.getUnitInfos());
		return SUCCESS;
	}

	/**
	 * @return unitInfo
	 */
	public UnitInfo getUnitInfo() {
		return unitInfo;
	}

	/**
	 * @param unitInfo 要设置的 unitInfo
	 */
	public void setUnitInfo(UnitInfo unitInfo) {
		this.unitInfo = unitInfo;
	}

	/**
	 * @return unitInfoList
	 */
	public List<UnitInfo> getUnitInfoList() {
		return unitInfoList;
	}

	/**
	 * @param unitInfoList 要设置的 unitInfoList
	 */
	public void setUnitInfoList(List<UnitInfo> unitInfoList) {
		this.unitInfoList = unitInfoList;
	}

	/**
	 * @return unitName
	 */
	public String getUnitName() {
		return unitName;
	}

	/**
	 * @param unitName 要设置的 unitName
	 */
	public void setUnitName(String unitName) {
		this.unitName = unitName;
	}

	/**
	 * @return unitInfoService
	 */
	public UnitInfoService getUnitInfoService() {
		return unitInfoService;
	}

	/**
	 * @param unitInfoService 要设置的 unitInfoService
	 */
	public void setUnitInfoService(UnitInfoService unitInfoService) {
		this.unitInfoService = unitInfoService;
	}


	protected void getDataList(DetachedCriteria query ){
		List<UnitInfo> result = new ArrayList<UnitInfo>();
		query.add(Restrictions.eq("delete_flag", false));
		int totalRows=RequestUtils.getTotalRowsFromRequest(getRequest());
		if(totalRows<0)
		{
			totalRows= unitInfoService.getUnitInfoCountByCriteria(query);
		}
		int[] rowStartEnd=RequestUtils.getRowStartEnd(getRequest(),totalRows,DEFAULT_PAGE_SIZE,0);
		query.addOrder(Order.asc("id"));
		query.setProjection(null);
		Limit limit=RequestUtils.getLimit(getRequest(),"ec",totalRows,DEFAULT_PAGE_SIZE);
		int pageSize=limit.getCurrentRowsDisplayed();
		result.addAll(unitInfoService.getUnitInfos(query, pageSize, rowStartEnd[0]));
		unitInfoList.addAll(result);		
		limit = RequestUtils.getLimit(getRequest(),"ec",result.size(),DEFAULT_PAGE_SIZE);
	}

}
